package com.chezhujie.www.service;

import com.chezhujie.www.domain.Czjcommodity;
import com.chezhujie.www.domain.vo.BusinessAndPic;
import com.chezhujie.www.persistence.CzjcommodityMapper;
import com.xy.core.exception.ClassCheckException;
import com.xy.user.base.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 商品表
 * Created by 汤伟杰 on 2017/5/23.
 */
@Service
public class CzjcommodityService extends BaseService<Czjcommodity> {
    @Autowired
    private CzjcommodityMapper czjcommodityMapper;

    @Autowired
    public void setBaseMapper(CzjcommodityMapper czjcommodityMapper) {
        this.baseMapper = czjcommodityMapper;
    }

    public List<Map<String,Object>> getListBusinessId(Map<String, Object> param) {
        return czjcommodityMapper.listBusinessId(param);
    }
    public List<Map<String,Object>> getListTypeId(Map<String, Object> param) {
        return czjcommodityMapper.listTypeId(param);
    }

    public void checkEntity(Czjcommodity czjcommodity) throws ClassCheckException {
        if(czjcommodity.getCommodityName()!=null&&czjcommodity.getCommodityName().length()>255){
            throw new ClassCheckException("商品表商品名称不能超过255个字符");
        }
        if(czjcommodity.getContent()!=null&&czjcommodity.getContent().length()>500){
            throw new ClassCheckException("商品表内容不能超过500个字符");
        }
    }
    public void checkListEntity(List<Czjcommodity> listCzjcommodity) throws ClassCheckException {

        for (Czjcommodity czjcommodity : listCzjcommodity) {
            checkEntity(czjcommodity);
        }

    }

    @Override
    public void insert(Czjcommodity czjcommodity) {
        czjcommodity.setApproveStatus(0);
        czjcommodity.setCreateTime(new Date());
        czjcommodity.setDelFlag(0);

        super.insert(czjcommodity);
    }
    /**
     * 修改审批状态
     * @param id
     * @param approveStatus
     */
    public void changeApprove(String id, Integer approveStatus) {
        Czjcommodity entity = queryById(id);
        entity.setApproveStatus(approveStatus);
        entity.setApproveTime(new Date());
        update(entity);
    }

	public BusinessAndPic getCommodity(String id) {
		
		return czjcommodityMapper.getCommodity(id);
	}

    public void updateDelByBusinessId(String businessId,Integer delFlag) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("businessId", businessId);
        param.put("delFlag",delFlag);
        czjcommodityMapper.updateDelByBusinessId(param);
    }

    public Map<String,Czjcommodity> getKeyBeanByIds(List<String> listcdstr) {
        List<Czjcommodity> list = czjcommodityMapper.queryByIds(listcdstr);
        Map<String, Czjcommodity> mapBean = new HashMap<String, Czjcommodity>();
        for (Czjcommodity czjcommodity : list) {
            mapBean.put(czjcommodity.getId(), czjcommodity);
        }
        return mapBean;
    }

    public void updates(List<Czjcommodity> listcd) {
        for (Czjcommodity czjcommodity : listcd) {
            czjcommodityMapper.update(czjcommodity);
        }
    }
    //商品头像、商品简介、现价、原价、商品名字、商品图片总数、评论总数
    public Map<String,Object> queryDetail(Map<String, Object> map) {
        return  czjcommodityMapper.queryDetail(map);
    }

    public List<Map<String,Object>> getCouponsList(Map<String, Object> param) {
        return  czjcommodityMapper.getCouponsList(param);
    }



}
